package com.warren.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.warren.annotation.ControllerEndpoint;
import com.warren.biz.entity.Consumer;
import com.warren.biz.entity.OutStock;
import com.warren.biz.entity.Product;
import com.warren.biz.service.ConsumerService;
import com.warren.biz.service.OutStockService;
import com.warren.biz.service.ProductService;
import com.warren.biz.vo.GetOutStockVO;
import com.warren.biz.vo.OutStockVO;
import com.warren.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;

import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author warren
 * @since 2021-04-24
 */
@RestController
@RequestMapping("/biz/outStock")
@Api(value = "物资出库接口")
public class OutStockController {

    @Resource
    private OutStockService outStockService;

    @Resource
    private ConsumerService consumerService;

    @Resource
    private ProductService productService;

    @ApiModelProperty(value = "分页查询入库列表")
    @PostMapping("/findOutStockList")
    public Result findOutStockList(@RequestParam(defaultValue = "1") Integer current,
                                   @RequestParam(defaultValue = "6") Integer size,
                                   @RequestBody OutStockVO outStockVO){
        System.out.println(outStockVO);
        System.out.println(current);
        System.out.println(size);
        //取得分页条件
        Page<OutStock> page = new Page<>(current,size);
        QueryWrapper<OutStock> queryWrapper = getWrapper(outStockVO);
        IPage<OutStock> productPage = outStockService.findOutStockList(page,queryWrapper);
        System.out.println(productPage);
        long total = productPage.getTotal();
        List<OutStock> records = productPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    @ControllerEndpoint(exceptionMessage = "查询物资出库单详细失败",operation = "物资出库单详细")
    @ApiOperation(value = "物资出库单详细")
    @PostMapping("/detail")
    public Result detail(@RequestParam String outNum,
                         @RequestParam Long consumerId,
                         @RequestParam(defaultValue = "1") Integer current,
                         @RequestParam(defaultValue = "3") Integer size){
        Page<Product> page = new Page<>(current,size);
        IPage<Product> productIPage = productService.getOutDetails(page,outNum);
        Consumer consumer = consumerService.getById(consumerId);

        long total = productIPage.getTotal();
        List<Product> records = productIPage.getRecords();
        return Result.ok().data("total",total).data("records",records).data("consumer",consumer);
    }

    @ControllerEndpoint(exceptionMessage = "入库单申请失败",operation = "入库单申请")
    @ApiOperation(value = "入库申请")
    @PostMapping("/addOutStock")
    public Result addOutStock(@RequestBody GetOutStockVO getOutStockVo){
        //新增来源
        if (getOutStockVo.getConsumerId() == null){
            Consumer consumer = new Consumer();
            consumer.setAddress(getOutStockVo.getAddress());
            consumer.setContact(getOutStockVo.getContact());
            consumer.setName(getOutStockVo.getName());
            consumer.setPhone(getOutStockVo.getPhone());
            consumer.setSort(getOutStockVo.getSort());
            Consumer consumer1 = consumerService.add(consumer);
            getOutStockVo.setConsumerId(consumer1.getId());
        }
        outStockService.addOutStock(getOutStockVo);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "恢复入库清单失败", operation = "恢复入库清单")
    @ApiOperation(value = "恢复入库清单")
    @PostMapping("/backOutStock")
    public Result backOutStock(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStock.setStatus(0);
        outStockService.updateById(outStock);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "移至回收站失败", operation = "移至回收站")
    @ApiOperation(value = "移至回收站")
    @PostMapping("/removeOutStock")
    public Result removeOutStock(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStock.setStatus(1);
        outStockService.updateById(outStock);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "审核失败", operation = "审核通过")
    @ApiOperation(value = "审核通过")
    @PostMapping("/publishOutStock")
    public Result publishOutStock(Long id,String outNum) {
        outStockService.publishOutStock( id, outNum);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "删除入库清单失败", operation = "删除入库清单")
    @ApiOperation(value = "删除入库清单")
    @DeleteMapping("/delOutStock")
    public Result delOutStock(Long id,String outNum) {
        outStockService.delete(id,outNum);
        return Result.ok();
    }

    private QueryWrapper<OutStock> getWrapper(OutStockVO outStockVo) {
        QueryWrapper<OutStock> queryWrapper = new QueryWrapper<>();
        if (outStockVo != null) {

            //查询每个条件
            if (!ObjectUtils.isEmpty(outStockVo.getStatus())) {
                queryWrapper.eq("status", outStockVo.getStatus());
            }
            if (!ObjectUtils.isEmpty(outStockVo.getOutNum())) {
                queryWrapper.like("out_num", outStockVo.getOutNum());
            }
            if (!ObjectUtils.isEmpty(outStockVo.getType())) {
                queryWrapper.eq("type", outStockVo.getType());
            }
            if (!ObjectUtils.isEmpty(outStockVo.getStartTime())) {
                queryWrapper.ge("o.create_time", outStockVo.getStartTime());
            }
            if (!ObjectUtils.isEmpty(outStockVo.getEndTime())) {
                queryWrapper.le("o.create_time", outStockVo.getEndTime());
            }
        }
        return queryWrapper;
    }
}

